import { Sprite, director, log } from "cc";

export class LotteryDebugInfo<T> {
    result: T[];
    get statistics() {
        let map = new Map<T, number>();
        for (let i = 0; i < this.result.length; i++) {
            let key = this.result[i];
            map.set(key, map.has(key) ? map.get(key) + 1 : 1);
        }
        map.forEach((v, k) => {
            if (k['toStr'])
                DB.log(`${k['toStr']()} :${v}\n`);
            else
                DB.log(`${k} :${v}\n`);
        })
        return map;
    }
    log() {
        if (this.result[0]['toStr'])
            DB.log(this.result.map(e => e['toStr']()));
        else
            DB.log(this.result);
    }


}
export default class DB {
    private static _isInDebug: boolean = false;
    static get isInDebug() { return this._isInDebug; }
    static set isInDebug(val: boolean) {
        this._isInDebug = val;
        // EC.send(E_RefreshDebug);;
    }
    static log(msg: string | any, ...subst: any[]) {
        log(msg, ...subst);
    }
    static labelTag(node: Node, str: string) {

    }
    static spriteTag(node: Node, str: string) {

    }
    //显示当前场景里所有脚本类
    static showUiDebug() {
        var res = {};
        var node_num = 0;
        var node_info = {};
        var label_num = 0;
        var label_info = {};
        var mapChild = function (children) {
            if (!children || !children.length) {
                return;
            }
            for (var i = 0; i < children.length; ++i) {
                var child = children[i];
                if (!node_info[child.name]) {
                    node_info[child.name] = 1;
                } else {
                    node_info[child.name]++;
                }
                node_num++;
                if (!child.active || !child.isValid) {
                    continue;
                }
                if (child._components) {
                    child._components.forEach((c) => {
                        // if (!(c.node && c.node.isValid)) {
                        // // if (!c.enabled || !(c.node && c.node.isValid)) {
                        //     return;
                        // }
                        var name = c.__classname__;
                        if (!res[name]) {
                            res[name] = 0;
                        }
                        if (name == 'Label') {
                            label_num++;
                            if (!label_info[child.parent.name]) {
                                label_info[child.parent.name] = 1;
                            } else {
                                label_info[child.parent.name]++;
                            }
                        }
                        res[name]++;
                    });
                }
                mapChild(child.children);
            }
        }
        mapChild(director.getScene().children);
        console.log('类详情:', res);
        console.log(`场景树上共有:${node_num}个node`);
        console.log(`节点详情:`, node_info);
        console.log(`场景树上共有:${label_num}个label`);
        console.log(`文本详情:`, label_info);
    }


    static showNodeDebug() {
        let tmp = director.getScene().getComponentsInChildren(Sprite);
        let count = 0;
        let t = [];
        let result = {};
        tmp.forEach(v => t.push(v.node));
        t.forEach(v => {
            if (!result[v.parent.name]) result[v.parent.name] = 1;
            else result[v.parent.name]++;
            count++;
        });
        console.log(`总共:${count}个节点,纹理父节点详情:`, result);
    }
    static testRandNumber<T>(randomFunc: () => T, num: number = 100) {
        let res: T[] = [];
        for (let i = 0; i < num; i++) {
            res.push(randomFunc());
        }
        let stat = new LotteryDebugInfo();
        stat.result = res;
        stat.log();
        stat.statistics;
    }

}
window['DB'] = DB;